package guiClasses;

/**
 * TCSS 360 - Project
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JScrollPane;

import objectClasses.Database;

/**
 *  The main frame that the user will see while using the application.
 *  The midPanel will display user specific information.
 *  Implements the Observer interface.
 *  
 *  @author Abraham Santini
 */

public class BeWeaveFrame extends JFrame implements Observer{
	/**
	 * A JPanel that displays user specific information.
	 */
	private JPanel midPanel;
	
	private JPanel outerMidPanel;
	
	private JPanel buttons;
	
	private JScrollPane scroll = new JScrollPane();
	
	private ArrayList<JPanel> backPanels = new ArrayList<JPanel>();
	
	private ArrayList<JPanel> forPanels = new ArrayList<JPanel>();
/**
 * Public constructor for the BeWeaveFrame.
 * Creates a BeWeaveFrame with midPanel set to a loginPanel.
 */
	public BeWeaveFrame() {
	setTitle("Beweave");

		setMinimumSize(new Dimension( 700, 850));
	
		this.setBackground(new Color(139, 101, 219));
		getContentPane().setLayout(new GridBagLayout());
		
		midPanel = new LoginPanel(this).getPanel();
		
		JLabel logoLabel = new JLabel(new ImageIcon("loom.jpeg"));
		getContentPane().add(logoLabel, GBC.GBCGen(0,0,0,0));
		JLabel BeWeaveLabel = new JLabel(new ImageIcon("weaveLogo.jpg"));
		getContentPane().add(BeWeaveLabel, GBC.GBCGen(0,0,1,0));
		
		updateButtons();
		
		scroll.setPreferredSize(new Dimension(650, 650));
		scroll.getViewport().add(midPanel);
		GridBagConstraints c = GBC.GBCGen(1,1,0,2);
		c.gridwidth = 3;
		c.gridheight = 1;
		getContentPane().add(scroll, c);
		JLabel spiritBeastLabel = new JLabel(new ImageIcon("Logo.jpg"));
		c = GBC.GBCGen(0,0,0,3);
		c.gridwidth = 3;
		c.anchor = GridBagConstraints.LAST_LINE_END;
		getContentPane().add(spiritBeastLabel, c);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		addWindowListener(new WindowListener(){

			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosed(WindowEvent arg0) {
				Database.saveFile();
				System.out.println("Frame has been closed");
				
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});

	} // end constructor
	
	
	public void updateButtons()
	{
		if(buttons != null)
		{
			remove(buttons);
		}
		buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));
		if(backPanels.size() > 0)
		{
			ImageIcon back = new ImageIcon("chrome-back-button.png");
			JButton backButton = new JButton(back);
			backButton.setPreferredSize(new Dimension(35, 28));
			backButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			backButton.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0) 
				{
					if(backPanels.size() > 0)
					{
						remove(scroll);
						scroll.getViewport().remove(midPanel);
						forPanels.add(midPanel);
						midPanel = backPanels.get(backPanels.size() - 1);
						invalidate();
						GridBagConstraints c = GBC.GBCGen(1,1,0,2);
						c.gridwidth = 3;
						c.gridheight = 1;
						scroll.getViewport().add(midPanel);
						getContentPane().add(scroll, c);
						backPanels.remove(backPanels.size() - 1);
						updateButtons();
						validate();
						repaint();
					}
				}
				
			});
			buttons.add(backButton);
		}
		else
		{
			ImageIcon back = new ImageIcon("Backward-not-selectable.png");
			JLabel backLabel = new JLabel(back);
			backLabel.setPreferredSize(new Dimension(35, 28));
			backLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			buttons.add(backLabel);
		}
		if(forPanels.size() > 0)
		{
			ImageIcon forward = new ImageIcon("chrome-forward-button.png");
			JButton forwardButton = new JButton(forward);
			forwardButton.setPreferredSize(new Dimension(35, 28));
			forwardButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			
			forwardButton.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0) 
				{
					if(forPanels.size() > 0)
					{
						remove(scroll);
						scroll.getViewport().remove(midPanel);
						backPanels.add(midPanel);
						midPanel = forPanels.get(forPanels.size() - 1);
						invalidate();
						GridBagConstraints c = GBC.GBCGen(1,1,0,2);
						c.gridwidth = 3;
						c.gridheight = 1;
						scroll.getViewport().add(midPanel);
						getContentPane().add(scroll, c);
						forPanels.remove(forPanels.size() - 1);
						updateButtons();
						validate();
						repaint();
					}
				}
				
			});
			buttons.add(forwardButton);
		}
		else
		{
			ImageIcon forward = new ImageIcon("Forward-not-selectable.png");
			JLabel forwardLabel = new JLabel(forward);
			forwardLabel.setPreferredSize(new Dimension(35, 28));
			forwardLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			buttons.add(forwardLabel);
		}
		GridBagConstraints g = GBC.GBCGen(0, 0, 0, 3);
		g.anchor = GridBagConstraints.WEST;
		getContentPane().add(buttons, g);
	}
	
	
	/**
	 * Public update method that switches out panels.
	 * @param arg1 This must be a JPanel.
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		forPanels.clear();
		backPanels.add(midPanel);
		remove(scroll);
		scroll.getViewport().remove(midPanel);
		midPanel = (JPanel) arg1;
		invalidate();
		GridBagConstraints c = GBC.GBCGen(1,1,0,2);
		c.gridwidth = 3;
		c.gridheight = 1;
		scroll.getViewport().add(midPanel);
		getContentPane().add(scroll, c);
		updateButtons();
		validate();
		repaint();
		
		
	}

}// end BeWeaveFrame class


